﻿using System;



namespace CSharp.Libs.Koc
{
    using CSharp.Libs.Koc;

    public static class Converters
    {
        /// <summary>Timestamp2DateString
        /// returns the given unix timestamp as string in a fix format.
        /// </summary>
        /// <param name="Timestamp">unix timestamp to be converted to string</param>
        /// <returns>string type of given unix timestamp in a fix format</returns>
        public static string Timestamp2DateString(
            this Kernel KRNL,
            Int32 Timestamp)
        {
            DateTime dateTime = Timestamp2Date(KRNL, Timestamp);
            return dateTime.ToShortDateString() + ", " + dateTime.ToShortTimeString();
        }

        /// <summary>Timestamp2Date
        /// returns the given unix timestamp as datetime type.
        /// </summary>
        /// <param name="Timestamp">unix timestamp to be converted to datetime type</param>
        /// <returns>datetime type of given unix timestamp</returns>
        /// <history>24.04.2011 (taken off http://dotnet-snippets.de/dns/unix-timestamp-in-datum-wandeln-SID164.aspx)</history>
        /// <history>modified 17.10.2011: put into own class as extension method</history>
        public static DateTime Timestamp2Date(
            this Kernel KRNL,
            Int32 Timestamp)
        {
            //  gerechnet wird ab der UNIX Epoche
            System.DateTime dateTime = new System.DateTime(1970, 1, 1, 0, 0, 0, 0);
            // den Timestamp addieren           
            dateTime = dateTime.AddSeconds(Timestamp);
            return dateTime;
        }

        /// <summary>Timestamp2Date
        /// returns the given unix timestamp as datetime type.
        /// </summary>
        /// <param name="Timestamp">unix timestamp to be converted to datetime type</param>
        /// <returns>datetime type of given unix timestamp</returns>
        /// <history>24.04.2011 (taken off http://dotnet-snippets.de/dns/unix-timestamp-in-datum-wandeln-SID164.aspx)</history>
        /// <history>modified 17.10.2011: put into own class as extension method</history>
        /// <history>modified 25.01.2012: version withouth kernel dependency</history>
        public static DateTime Timestamp2Date(
            Int32 Timestamp)
        {
            //  gerechnet wird ab der UNIX Epoche
            System.DateTime dateTime = new System.DateTime(1970, 1, 1, 0, 0, 0, 0);
            // den Timestamp addieren           
            dateTime = dateTime.AddSeconds(Timestamp);
            return dateTime;
        }

        /// <summary>Date2Timestamp
        /// return the unix timestamp of a given DateTime type.
        /// </summary>
        /// <param name="date">DateTime type to be converted to unix timestamp</param>
        /// <returns>Unix timestamp of the given DateTime type</returns>
        /// <history>24.04.2011 (taken off http://dotnet-snippets.de/dns/unix-timestamp-in-datum-wandeln-SID164.aspx)</history>
        /// <history>modified 17.10.2011: put into own class as extension method</history>
        public static int Date2Timestamp(
            this Kernel KRNL,
            DateTime date)
        {
            DateTime date1 = new DateTime(1970, 1, 1);  //Refernzdatum (festgelegt)
            TimeSpan ts = new TimeSpan(date.Ticks - date1.Ticks);  // das Delta ermitteln
            // Das Delta als gesammtzahl der sekunden ist der Timestamp
            return (Convert.ToInt32(ts.TotalSeconds));
        }

        /// <summary>Date2Timestamp
        /// return the unix timestamp of a given DateTime type.
        /// </summary>
        /// <param name="date">DateTime type to be converted to unix timestamp</param>
        /// <returns>Unix timestamp of the given DateTime type</returns>
        /// <history>24.04.2011 (taken off http://dotnet-snippets.de/dns/unix-timestamp-in-datum-wandeln-SID164.aspx)</history>
        /// <history>modified 17.10.2011: put into own class as extension method</history>
        /// <history>modified 25.01.2012: version withouth kernel dependency</history>
        public static int Date2Timestamp(
            DateTime date)
        {
            DateTime date1 = new DateTime(1970, 1, 1);  //Refernzdatum (festgelegt)
            TimeSpan ts = new TimeSpan(date.Ticks - date1.Ticks);  // das Delta ermitteln
            // Das Delta als gesammtzahl der sekunden ist der Timestamp
            return (Convert.ToInt32(ts.TotalSeconds));
        }
    }
}
